import 'dart:developer';

import 'package:huxiaodui/config.dart';
import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:amap_flutter_location/amap_location_option.dart';

class Position {
  /// 单例模式
  static final Position _instance = Position._internal();
  factory Position() {
    return _instance;
  }
  Position._internal();
  AMapFlutterLocation aMapFlutterLocation = AMapFlutterLocation();

  Future<AMapFlutterLocation> init(
      {int locationInterval = 10 * 1000,
      required Function(Map<String, Object>) onEvent}) async {
    aMapFlutterLocation = AMapFlutterLocation();
    AMapFlutterLocation.updatePrivacyShow(true, true);
    AMapFlutterLocation.updatePrivacyAgree(true);
    AMapFlutterLocation.setApiKey(AmapConfig.androidKey, AmapConfig.iosKey);

    aMapFlutterLocation.onLocationChanged().listen((Map<String, Object> data) {
      if (data["latitude"] != null && data["longitude"] != null) {
        log("定位：$data");
        onEvent(data);
      }
    });
    aMapFlutterLocation.setLocationOption(
        AMapLocationOption(locationInterval: locationInterval));

    aMapFlutterLocation.startLocation();

    return aMapFlutterLocation;
  }

  void stop() async {
    aMapFlutterLocation.stopLocation();
  }

  void destroy() async {
    aMapFlutterLocation.destroy();
  }

  void start(){
    aMapFlutterLocation.startLocation();
  }
}
